求教关于linux的堆栈设置 |
您所在的位置:网站首页 › ulimit 临时修改 › 求教关于linux的堆栈设置 |
在/etc/profile 的最后面添加ulimit -s unlimited 保存,source /etc/profile使修改文件生效 linux查看修改线程默认栈空间大小 :ulimit -s 1、通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M 2、通过命令 ulimit -s 设置大小值 临时改变栈空间大小:ulimit -s 102400, 即修改为100M 3、可以在/etc/rc.local 内 加入 ulimit -s 102400 则可以开机就设置栈空间大小 4、在/etc/security/limits.conf 中也可以改变栈空间大小: #domaintypeitemvalue * soft stack 102400 重新登录,执行ulimit -s 即可看到改为102400 即100M 【缓冲区溢出的处理】你屋子里的门和窗户越少,入侵者进入的方式就越少…… 由于缓冲区溢出是一个编程问题,所以只能通过修复被破坏的程序的代码而解决问题。如果你没有源代码,从上面“堆栈溢出攻击”的原理可以看出,要防止此类攻击,我们可以: ① 开放程序时仔细检查溢出情况,不允许数据溢出缓冲区。由于编程和编程语言的原因,这非常困难,而且不适合大量已经在使用的程序; ② 使用检查堆栈溢出的编译器或者在程序中加入某些记号,以便程序运行时确认禁止黑客有意造成的溢出。问题是无法针对已有程序,对新程序来讲,需要修改编译器; ③ 经常检查你的操作系统和应用程序提供商的站点,一旦发现他们提供的补丁程序,就马上下载并且应用在系统上,这是最好的方法。但是系统管理员总要比攻击者慢 一步,如果这个有问题的软件是可选的,甚至是临时的,把它从你的系统中删除。举另外一个例 子,你屋子里的门和窗户越少,入侵者进入的方式就越少。 ---------------------------------------------------------------------------------------------------------------------------------------- char buf[3] memset(buf,0x55,10) 这个程序就存在溢出 对数据块的访问超出该数据块的地址范围 =================================================================================== 【一个检测工具】 Valgrind 是一款 Linux下(支持 x86、x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的 malloc 和 free,以及 C++ 中的 new 和 delete),找出内存泄漏问题。 Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误: 使用未初始化的内存 (Use of uninitialised memory) 使用已经释放了的内存 (Reading/writing memory after it has been free’d) 使用超过 malloc 分配的内存空间(Reading/writing off the end of malloc’d blocks) 对堆栈的非法访问(Reading/writing inappropriate areas on the stack) 申请的空间是否有释放(Memory leaks – where pointers to malloc’d blocks are lost forever) malloc/free/new/delete 申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete []) src 和 dst 的重叠(Overlapping src and dst pointers in memcpy() and related functions) 重复 free ① 编译安装 Valgrind: # wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2 # tar xvf valgrind-3.4.1.tar.bz2 # cd valgrind-3.4.1/ # ./configure ………… Primary build target: X86_LINUX Secondary build target: Default supp files: exp-ptrcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.5.supp # make # make install # whereis valgrind valgrind: /usr/bin/valgrind /usr/lib/valgrind /usr/local/bin/valgrind /usr/local/lib/valgrind /usr/include/valgrind /usr/share/man/man1/valgrind.1.gz 运行程序 使用示例:对“ls”程序进程检查,返回结果中的“definitely lost: 0 bytes in 0 blocks.”表示没有内存泄漏。 # /usr/local/bin/valgrind --tool=memcheck --leak-check=full ls / ==29801== Memcheck, a memory error detector. ==29801== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al. ==29801== Using LibVEX rev 1884, a library for dynamic binary translation. ==29801== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP. ==29801== Using valgrind-3.4.1, a dynamic binary instrumentation framework. ==29801== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al. ==29801== For more details, rerun with: -v ==29801== bin etc lost+found mnt proc selinuxsys usr boot home media net root smokeping tftpboot var dev lib miscopt sbin srvtmp ==29801== ==29801== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 1) ==29801== malloc/free: in use at exit: 14,744 bytes in 32 blocks. ==29801== malloc/free: 162 allocs, 130 frees, 33,758 bytes allocated. ==29801== For counts of detected errors, rerun with: -v ==29801== searching for pointers to 32 not-freed blocks. ==29801== checked 139,012 bytes. ==29801== ==29801== LEAK SUMMARY: ==29801==definitely lost: 0 bytes in 0 blocks. ==29801== possibly lost: 0 bytes in 0 blocks. ==29801==still reachable: 14,744 bytes in 32 blocks. ==29801== suppressed: 0 bytes in 0 blocks. ==29801== Reachable blocks (those to which a pointer was found) are not shown. ==29801== To see them, rerun with: --leak-check=full --show-reachable=yes ---------------------------------------------------------------------------------------------------------------------------------------- # /usr/local/bin/valgrind --tool=memcheck --leak-check=full ps / ==29898== Memcheck, a memory error detector. ==29898== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al. ==29898== Using LibVEX rev 1884, a library for dynamic binary translation. ==29898== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP. ==29898== Using valgrind-3.4.1, a dynamic binary instrumentation framework. ==29898== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al. ==29898== For more details, rerun with: -v ==29898== ERROR: Garbage option. ********* simple selection ********* ********* selection by list ********* -A all processes -C by command name -N negate selection -G by real group ID (supports names) -a all w/ tty except session leaders -U by real user ID (supports names) -d all except session leaders -g by session OR by effective group name -e all processes -p by process ID T all processes on this terminal -s processes in the sessions given a all w/ tty, including other users -t by tty g OBSOLETE -- DO NOT USE -u by effective user ID (supports names) r only running processes U processes for specified users x processes w/o controlling ttys t by tty *********** output format ********** *********** long options *********** -o,o user-defined -f full--Group --User --pid --cols --ppid -j,j job control s signal --group --user --sid --rows --info -O,O preloaded -o v virtual memory --cumulative --format --deselect -l,l long u user-oriented --sort --tty --forest --version -F extra fullX registers --heading --no-heading --context ********* misc options ********* -V,V show version L list format codes f ASCII art forest -m,m,-L,-T,H threads S children in sum-y change -l format -M,Z security data c true command name -c scheduling class -w,w wide output n numeric WCHAN,UID -H process hierarchy ==29898== ==29898== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 14 from 1) ==29898== malloc/free: in use at exit: 16 bytes in 2 blocks. ==29898== malloc/free: 20 allocs, 18 frees, 2,344 bytes allocated. ==29898== For counts of detected errors, rerun with: -v ==29898== searching for pointers to 2 not-freed blocks. ==29898== checked 263,972 bytes. ==29898== ==29898== 8 bytes in 1 blocks are definitely lost in loss record 2 of 2 ==29898==at 0x4005A88: malloc (vg_replace_malloc.c:207) ==29898==by 0xBFF6DF: strdup (in /lib/libc-2.5.so) ==29898==by 0x804A464: (within /bin/ps) ==29898==by 0x804A802: (within /bin/ps) ==29898==by 0x804964D: (within /bin/ps) ==29898==by 0xBA5E8B: (below main) (in /lib/libc-2.5.so) ==29898== ==29898== LEAK SUMMARY: ==29898==definitely lost: 8 bytes in 1 blocks. ==29898== possibly lost: 0 bytes in 0 blocks. ==29898==still reachable: 8 bytes in 1 blocks. ==29898== suppressed: 0 bytes in 0 blocks. ==29898== Reachable blocks (those to which a pointer was found) are not shown. ==29898== To see them, rerun with: --leak-check=full --show-reachable=yes 一、预备知识—程序的内存分配一个由C/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其 操作方式类似于数据结构中的栈。 2、堆区(heap) — 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回 收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。 3、全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的 全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另 一块区域。 - 程序结束后由系统释放。 4、文字常量区 —常量字符串就是放在这里的。 程序结束后由系统释放 5、程序代码区—存放函数体的二进制代码。 二、例子程序 这是一个前辈写的,非常详细 //main.cpp int a = 0 全局初始化区 char *p1 全局未初始化区 main() { int b 栈 char s[] = "abc" 栈 char *p2 栈 char *p3 = "123456" 123456/0在常量区,p3在栈上。 static int c =0; 全局(静态)初始化区 p1 = (char *)malloc(10) p2 = (char *)malloc(20) 分配得来得10和20字节的区域就在堆区。 strcpy(p1, "123456") 123456/0放在常量区,编译器可能会将它与p3所指向的"123456" 优化成一个地方。 } 二、堆和栈的理论知识 2.1申请方式 stack: 由系统自动分配。 例如,声明在函数中一个局部变量 int b 系统自动在栈中为b开辟空 间 heap: 需要程序员自己申请,并指明大小,在c中malloc函数 如p1 = (char *)malloc(10) 在C++中用new运算符 如p2 = new char[10] 但是注意p1、p2本身是在栈中的。 2.2 申请后系统的响应 栈:只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢 出。 堆:首先应该知道操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时, 会遍历该链表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表 中删除,并将该结点的空间分配给程序,另外,对于大多数系统,会在这块内存空间中的 首地址处记录本次分配的大小,这样,代码中的delete语句才能正确的释放本内存空间。 另外,由于找到的堆结点的大小不一定正好等于申请的大小,系统会自动的将多余的那部 分重新放入空闲链表中。 2.3申请大小的限制 栈:在Windows下,栈是向低地址扩展的数据结构,是一块连续的内存的区域。这句话的意 思是栈顶的地址和栈的最大容量是系统预先规定好的,在WINDOWS下,栈的大小是2M(也有 的说是1M,总之是一个编译时就确定的常数),如果申请的空间超过栈的剩余空间时,将 提示overflow。因此,能从栈获得的空间较小。 堆:堆是向高地址扩展的数据结构,是不连续的内存区域。这是由于系统是用链表来存储 的空闲内存地址的,自然是不连续的,而链表的遍历方向是由低地址向高地址。堆的大小 受限于计算机系统中有效的虚拟内存。由此可见,堆获得的空间比较灵活,也比较大。 2.4申请效率的比较: 栈由系统自动分配,速度较快。但程序员是无法控制的。 堆是由new分配的内存,一般速度比较慢,而且容易产生内存碎片,不过用起来最方便. 另外,在WINDOWS下,最好的方式是用VirtualAlloc分配内存,他不是在堆,也不是在栈是 直接在进程的地址空间中保留一块内存,虽然用起来最不方便。但是速度快,也最灵活。 2.5堆和栈中的存储内容 栈: 在函数调用时,第一个进栈的是主函数中后的下一条指令(函数调用语句的下一条可 执行语句)的地址,然后是函数的各个参数,在大多数的C编译器中,参数是由右往左入栈 的,然后是函数中的局部变量。注意静态变量是不入栈的。 当本次函数调用结束后,局部变量先出栈,然后是参数,最后栈顶指针指向最开始存的地 址,也就是主函数中的下一条指令,程序由该点继续运行。 堆:一般是在堆的头部用一个字节存放堆的大小。堆中的具体内容由程序员安排。 2.6存取效率的比较 char s1[] = "aaaaaaaaaaaaaaa" char *s2 = "bbbbbbbbbbbbbbbbb" aaaaaaaaaaa是在运行时刻赋值的; 而bbbbbbbbbbb是在编译时就确定的; 但是,在以后的存取中,在栈上的数组比指针所指向的字符串(例如堆)快。 比如: #include void main() { char a = 1 char c[] = "1234567890" char *p ="1234567890" a = c[1] a = p[1] return } 对应的汇编代码 10: a = c[1] 00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh] 0040106A 88 4D FC mov byte ptr [ebp-4],cl 11: a = p[1] 0040106D 8B 55 EC mov edx,dword ptr [ebp-14h] 00401070 8A 42 01 mov al,byte ptr [edx+1] 00401073 88 45 FC mov byte ptr [ebp-4],al 第一种在读取时直接就把字符串中的元素读到寄存器cl中,而第二种则要先把指针值读到 edx中,再根据edx读取字符,显然慢了。 2.7小结: 堆和栈的区别可以用如下的比喻来看出: 使用栈就象我们去饭馆里吃饭,只管点菜(发出申请)、付钱、和吃(使用),吃饱了就 走,不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作,他的好处是快捷,但是自 由度小。 使用堆就象是自己动手做喜欢吃的菜肴,比较麻烦,但是比较符合自己的口味,而且自由 欢迎分享,转载请注明来源:内存溢出 原文地址:https://outofmemory.cn/yw/8594096.html |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |